package com.example.demo.mapper;

import com.example.demo.model.UserLevel;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserLevelMapper {
    @Select("SELECT * FROM user_level WHERE user_id = #{userId}")
    UserLevel selectUserLevelByUserId(Long userId);

    @Insert("INSERT INTO user_level (user_id, level, experience) VALUES (#{userId}, #{level}, #{experience})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertUserLevel(Long userId, int level, int experience);

    @Insert("UPDATE user_level SET level = #{level}, experience = #{experience} WHERE user_id = #{userId}")
    void updateUserLevel(Long userId, int level, int experience);

    @Insert("DELETE FROM user_level WHERE user_id = #{userId}")
    void deleteUserLevel(Long userId);

    @Insert("UPDATE user_level SET experience = experience + #{experience} WHERE user_id = #{userId}")
    void addExperience(Long userId, int experience);

    @Insert("UPDATE user_level SET level = level + 1 WHERE user_id = #{userId} AND experience >= (level * 100)")
    void levelUp(Long userId);

    @Insert("UPDATE user_level SET level = level - 1 WHERE user_id = #{userId} AND level > 1")
    void levelDown(Long userId);

    @Insert("UPDATE user_level SET filesize = fileszie + #{filesize} WHERE user_id = #{userId}")
    void addFilesize(Long userId, int filesize);

    @Select("SELECT * FROM user_level WHERE filesize > " +
            "CASE level " +
            "WHEN 1 THEN 50 " +
            "WHEN 2 THEN 100 " +
            "WHEN 3 THEN 200 " +
            "WHEN 4 THEN 500 " +
            "ELSE 0 END")
    List<UserLevel> selectByFilesizeGreaterThanLevel();

    @Insert("UPDATE user_level SET share_rate = #{shareRate} WHERE user_id = #{userId}")
    void updateShareRate(Long userId, float shareRate);
}
